package com.cn.learn.creator.controller;


import com.cn.learn.creator.domain.entity.GenTable;
import com.cn.learn.creator.domain.entity.GenTableColumn;
import com.cn.learn.creator.domain.vo.ResultVO;
import com.cn.learn.creator.ruoyi.text.Convert;
import com.cn.learn.creator.service.IGenTableColumnService;
import com.cn.learn.creator.service.IGenTableService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代码生成 操作处理
 *
 * @author ruoyi
 */
@RequestMapping("/gen")
@RestController
public class GenController {
    @Autowired
    private IGenTableService genTableService;

    @Autowired
    private IGenTableColumnService genTableColumnService;


    //修改保存代码生成业务
    @PutMapping
    public void editSave(@Validated @RequestBody GenTable genTable) {
        genTableService.validateEdit(genTable);
        genTableService.updateGenTable(genTable);
    }


    // 修改代码生成业务的回显数据
    @GetMapping(value = "/{tableId}")
    public ResultVO<Map<String, Object>> getInfo(@PathVariable Long tableId) {
        GenTable table = genTableService.selectGenTableById(tableId);
        List<GenTable> tables = genTableService.selectGenTableAll();
        List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(tableId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("info", table);
        map.put("rows", list);
        map.put("tables", tables);
        return ResultVO.getResultOnlyData(map);
    }


    //预览代码
    @GetMapping("/preview/{tableId}")
    public ResultVO<Map<String, String>> preview(@PathVariable("tableId") Long tableId) throws IOException {
        Map<String, String> dataMap = genTableService.previewCode(tableId);
        return ResultVO.getResultOnlyData(dataMap);
    }


    //  生成代码（自定义路径）
    @GetMapping("/genCode/{tableName}")
    public void genCode(@PathVariable("tableName") String tableName) {
        genTableService.generatorCode(tableName);

    }


    // 生成zip文件
    private void genCode(HttpServletResponse response, byte[] data) throws IOException {
        //TODO zip文生成3
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"ruoyi.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }


    //生成单个表的代码（下载zip方式）
    @GetMapping("/download/{tableName}")
    public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException {
        byte[] data = genTableService.downloadCode(tableName);
        genCode(response, data);
    }

    //批量生成zip文件的代码
    @GetMapping("/batchGenCode")
    public void batchGenCode(HttpServletResponse response, String tables) throws IOException {
        String[] tableNames = Convert.toStrArray(tables);
        byte[] data = genTableService.downloadCode(tableNames);
        genCode(response, data);
    }


    //同步数据库
    @PutMapping("/synchDb/{genTableId}")
    public void synchDb(@PathVariable("genTableId") Long tableId) {
        genTableService.synchDb(tableId);

    }


    // 删除代码生成
    @DeleteMapping("/{tableIds}")
    public void remove(@PathVariable Long[] tableIds) {
        genTableService.deleteGenTableByIds(tableIds);
    }


    //查询代码生成列表
    @GetMapping("/list")
    public ResultVO<List<GenTable>> genList(@ModelAttribute GenTable genTable) {
        return genTableService.selectGenTableList(genTable);
    }


    //导入表结构（保存）
    @PostMapping("/importTable")
    public void importTableSave(String tables) {
        String[] tableNames = Convert.toStrArray(tables);
        // 查询表信息
        List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames);
        genTableService.importGenTable(tableList);
    }


    /**
     * 查询数据库列表
     */
    @GetMapping("/db/list")
    public ResultVO<List<GenTable>> dataList(GenTable genTable) {
        List<GenTable> list = genTableService.selectDbTableList(genTable);
        return ResultVO.getResultWithCountByList(list);
    }

    /**
     * 查询数据表字段列表
     */
    @GetMapping(value = "/column/{tableId}")
    public ResultVO<List<GenTableColumn>> columnList(Long tableId) {
        List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(tableId);

        return ResultVO.getResultWithCountByList(list);
    }


}
